package com.aistar.service.impl;

import com.aistar.mapper.ChoiceMapper;
import com.aistar.mapper.QuestionSetMapper;
import com.aistar.pojo.*;
import com.aistar.service.ChoiceService;
import com.aistar.util.MessageUtil;
import com.aistar.util.ServerResponse;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ChoiceServiceImpl implements ChoiceService {

    @Autowired
    private ChoiceMapper choiceMapper;

    //SELECT * FROM `choice` where choice_keywords ="C|C++"
    // or choice_keywords="Java" or choice_keywords ="C";
    //根据选择的关键字查询套题       C|C++,Java,C
    @Override
    public ServerResponse getByKw(String[] kw) {
        PageHelper.startPage(1, 10);
        ChoiceExample choiceExample = new ChoiceExample();
        choiceExample.setOrderByClause("rand()");
        for (String kw1 : kw) {
            ChoiceExample.Criteria criteria = choiceExample.createCriteria();
            choiceExample.or(criteria.andChoiceKeywordsLike(kw1.trim()));
        }
        List<Choice> choiceList = choiceMapper.selectByExample(choiceExample);
        if (choiceList != null && choiceList.size() > 0)
            return ServerResponse.getSuccess(choiceList);
        return ServerResponse.getFailed();
    }


    @Override
    public ServerResponse getAll() {
        ChoiceExample example = new ChoiceExample();
        List<Choice> choiceList = choiceMapper.selectByExample(example);
        System.out.println(choiceList);
        if (choiceList != null && choiceList.size() > 0)
            return ServerResponse.getSuccess(choiceList);
        return ServerResponse.getFailed();
    }

    @Override
    public ServerResponse getById(Integer choiceId) {
        Choice choice = choiceMapper.selectByPrimaryKey(choiceId);
        if (choice != null)
            return ServerResponse.getSuccess(choice);
        return ServerResponse.getFailed();
    }


}
